Calculated Distance Using Haversine Formula

Select Origin In The Map

Result:

First Point on Google Maps
Second Point on Google Maps
from js import document, alert from pyodide import create_proxy from math import radians, cos, sin, asin, sqrt def button_click(event): FirstLat = document.getElementById("FirstLat").value FirstLong = document.getElementById("FirstLong").value SecLat = document.getElementById("SecLat").value SecLong = document.getElementById("SecLong").value try: # Convert string inputs to float and then to radians first_lat = radians(float(FirstLat)) first_long = radians(float(FirstLong)) sec_lat = radians(float(SecLat)) sec_long = radians(float(SecLong)) except ValueError: alert("Invalid inputs. Please select valid coordinates.") return False # Haversine formula calculation dlat = sec_lat - first_lat dlon = sec_long - first_long a = sin(dlat / 2)**2 + cos(first_lat) * cos(sec_lat) * sin(dlon / 2)**2 c = 2 * asin(sqrt(a)) r = 6371 # Earth's radius in kilometers calculated_distance = c * r document.getElementById("Distance").innerText = f"Calculated Distance: {calculated_distance:.2f} km" def setup(): click_proxy = create_proxy(button_click) document.getElementById("button").addEventListener("click", click_proxy) setup()

To calculate the distance based on a network of roads with a UI, please click the following link!

Distance Calculator Based on a Network of Roads